#ifdef SUPPORT_MS_EXTENSIONS
#pragma bss_seg(".usb.data.bss")
#pragma data_seg(".usb.data")
#pragma code_seg(".usb.text")
#pragma const_seg(".usb.text.const")
#pragma str_literal_override(".usb.text.const")
#endif

#include "system/includes.h"
#include "os/os_api.h"
#include "usb/device/usb_stack.h"
#include "usb_config.h"

#include "app_config.h"

#if TCFG_USB_SLAVE_MIDI_ENABLE

#define LOG_TAG_CONST       USB
#define LOG_TAG             "[USB]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"

static u8 *midi_dma_tx_buffer;
static u8 *midi_dma_rx_buffer;

static const u8 sMidiDescriptor[] = {
    0x09,        // bLength
    0x04,        // bDescriptorType (Interface)
    0x00,        // bInterfaceNumber 0
    0x00,        // bAlternateSetting
    0x00,        // bNumEndpoints 0
    0x01,        // bInterfaceClass (Audio)
    0x01,        // bInterfaceSubClass (Audio Control)
    0x00,        // bInterfaceProtocol
    0x00,        // iInterface (String Index)

    0x09,        // bLength
    0x24,        // bDescriptorType (See Next Line)
    0x01,        // bDescriptorSubtype (CS_INTERFACE -> HEADER)
    0x00, 0x01,  // bcdADC 1.00
    0x09, 0x00,  // wTotalLength 9
    0x01,        // binCollection 0x01
    0x01,        // baInterfaceNr 1

    0x09,        // bLength
    0x04,        // bDescriptorType (Interface)
    0x01,        // bInterfaceNumber 1
    0x00,        // bAlternateSetting
    0x02,        // bNumEndpoints 2
    0x01,        // bInterfaceClass (Audio)
    0x03,        // bInterfaceSubClass
    0x00,        // bInterfaceProtocol
    0x00,        // iInterface (String Index)

    0x07,        // bLength
    0x24,        // bDescriptorType (See Next Line)
    0x01, 0x00, 0x01, 0x25, 0x00,
    0x06,        // bLength
    0x24,        // bDescriptorType (See Next Line)
    0x02, 0x01, 0x01, 0x00,
    0x06,        // bLength
    0x24,        // bDescriptorType (See Next Line)
    0x02, 0x02, 0x02, 0x00,
    0x09,        // bLength
    0x24,        // bDescriptorType (See Next Line)
    0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
    0x09,        // bLength
    0x24,        // bDescriptorType (See Next Line)
    0x03, 0x02, 0x04, 0x01, 0x01, 0x01, 0x00,

    0x07,        // bLength
    0x05,        // bDescriptorType (See Next Line)
    MIDI_EP_IN | USB_DIR_IN,        // bEndpointAddress (IN/D2H)
    USB_ENDPOINT_XFER_BULK,        // bmAttributes (Bulk)
    LOBYTE(MAXP_SIZE_MIDI_EPIN), HIBYTE(MAXP_SIZE_MIDI_EPIN),  // wMaxPacketSize 64
    0x00,        // bInterval 0 (unit depends on device speed)

    0x05,        // bLength
    0x25,        // bDescriptorType (See Next Line)
    0x01, 0x01, 0x03,

    0x07,        // bLength
    0x05,        // bDescriptorType (See Next Line)
    MIDI_EP_OUT,                    // bEndpointAddress (OUT/H2D)
    USB_ENDPOINT_XFER_BULK,         // bmAttributes (Bulk)
    LOBYTE(MAXP_SIZE_MIDI_EPOUT), HIBYTE(MAXP_SIZE_MIDI_EPOUT),  // wMaxPacketSize 64
    0x00,        // bInterval 0 (unit depends on device speed)

    0x05,        // bLength
    0x25,        // bDescriptorType (See Next Line)
    0x01, 0x01, 0x01,
// 88 bytes
};

static void (*usb_midi_rx_notify)(const usb_dev usb_id);

void midi_register_rx_notify(void (*func)(const usb_dev usb_id))
{
    usb_midi_rx_notify = func;
}
static void midi_ep_rx_handler(struct usb_device_t *usb_device, u32 ep)
{
    const usb_dev usb_id = usb_device2id(usb_device);
    if (usb_midi_rx_notify) {
        usb_midi_rx_notify(usb_id);
    }
}
static void midi_endpoint_init(struct usb_device_t *usb_device, u32 itf)
{
    const usb_dev usb_id = usb_device2id(usb_device);
    u8 *ep_buffer;
    ep_buffer = midi_dma_tx_buffer;
    usb_g_ep_config(usb_id, MIDI_EP_IN | USB_DIR_IN, USB_ENDPOINT_XFER_BULK, 0, ep_buffer, MAXP_SIZE_MIDI_EPIN);

    ep_buffer = midi_dma_rx_buffer;
    usb_g_ep_config(usb_id, MIDI_EP_OUT, USB_ENDPOINT_XFER_BULK, 1, ep_buffer, MAXP_SIZE_MIDI_EPOUT);
    usb_g_set_intr_hander(usb_id, MIDI_EP_OUT, midi_ep_rx_handler);

    usb_enable_ep(usb_id, MIDI_EP_IN);
    //log_info("midi tx dma buffer %x", ep_buffer);
}
static void midi_reset(struct usb_device_t *usb_device, u32 itf)
{
    const usb_dev usb_id = usb_device2id(usb_device);
    log_debug("%s", __func__);
    midi_endpoint_init(usb_device, itf);
}
u32 midi_tx_data(const usb_dev usb_id, const u8 *buffer, u32 len)
{
    log_info_hexdump((u8 *)buffer, len);
    return usb_g_bulk_write(usb_id, MIDI_EP_IN, buffer, len);
}
u32 midi_rx_data(const usb_dev usb_id, u8 *buffer)
{
    return usb_g_bulk_read(usb_id, MIDI_EP_OUT, buffer, MAXP_SIZE_MIDI_EPOUT, 0);
}
static u32 midi_itf_hander(struct usb_device_t *usb_device, struct usb_ctrlrequest *req)
{
    const usb_dev usb_id = usb_device2id(usb_device);
    u8 *tx_payload = usb_get_setup_buffer(usb_device);
    u32 bRequestType = req->bRequestType & USB_TYPE_MASK;
    switch (bRequestType) {
    case USB_TYPE_STANDARD:
        break;
    }
    return 0;
}
u32 midi_desc_config(const usb_dev usb_id, u8 *ptr, u32 *cur_itf_num)
{
    log_debug("midi interface num:%d\n", *cur_itf_num);
    memcpy(ptr, sMidiDescriptor, sizeof(sMidiDescriptor));
    ptr[2] = *cur_itf_num;
    ptr[9 + 9 + 2] = *cur_itf_num + 1;
    if (usb_set_interface_hander(usb_id, *cur_itf_num, midi_itf_hander) != *cur_itf_num) {
        ASSERT(0, "midi set interface_hander fail");
    }
    if (usb_set_reset_hander(usb_id, *cur_itf_num, midi_reset) != *cur_itf_num) {
        ASSERT(0, "midi set interface_reset_hander fail");
    }

    *cur_itf_num = *cur_itf_num + 2;
    return sizeof(sMidiDescriptor) ;
}

void midi_register(const usb_dev usb_id)
{
    if (!midi_dma_tx_buffer) {
        midi_dma_tx_buffer = usb_alloc_ep_dmabuffer(usb_id, MIDI_EP_IN | USB_DIR_IN, MAXP_SIZE_MIDI_EPIN);
    }
    if (!midi_dma_rx_buffer) {
        midi_dma_rx_buffer = usb_alloc_ep_dmabuffer(usb_id, MIDI_EP_OUT, MAXP_SIZE_MIDI_EPOUT);
    }
}

void midi_release(const usb_dev usb_id)
{
    if (midi_dma_tx_buffer) {
        usb_free_ep_dmabuffer(usb_id, midi_dma_tx_buffer);
        midi_dma_tx_buffer = NULL;
    }
    if (midi_dma_rx_buffer) {
        usb_free_ep_dmabuffer(usb_id, midi_dma_rx_buffer);
        midi_dma_rx_buffer = NULL;
    }
}

///demo code
//
#define BT1 32,24


static const u8  SongTable[] = {
    373 / 256, 373 % 256,

//引子
    1, 83, 70, BT1 / 2, //7
    2, 83, 0, 84, 90, BT1 / 2, //1
    2, 84, 0, 86, 90, BT1 / 2, //2
    2, 86, 0, 91, 90, BT1 / 2, //5
    2, 91, 0, 89, 90, BT1 / 2, //4
    2, 89, 0, 81, 90, BT1 / 2, //6
    2, 81, 0, 79, 90, BT1 / 2, //5
    2, 79, 0, 88, 90, BT1 / 2, //3

    2, 88, 0, 86, 90, BT1 / 2, //2
    2, 86, 0, 79, 90, BT1 / 2, //5
    2, 79, 0, 77, 90, BT1 / 2, //4
    2, 77, 0, 84, 90, BT1 / 2, //1
    2, 84, 0, 83, 90, BT1 / 2, //7
    2, 83, 0, 76, 90, BT1 / 2, //3
    2, 76, 0, 79, 90, BT1 / 2, //5
    2, 79, 0, 86, 90, BT1 / 2, //2

    3, 0xFF, 58, 120, 0xFF, 86, 0, 91, 90, BT1 * 4, //5

//第一小节
//C和弦
    2, 91, 0, 48, 70, BT1 / 2,
    1, 55, 70, BT1 / 2,
    2, 55, 0, 60, 70, BT1 / 2,
    2, 60, 0, 64, 70, BT1 / 2,
//C和弦
    1, 64, 70, BT1 / 2,
    1, 55, 70, BT1 / 2,
    4, 48, 0, 55, 0,  60, 70, 55, 100, BT1 / 2, //5
    4, 60, 0, 55, 0,  64, 70, 57, 100, BT1 / 2, //6

//第二小节
//C和弦
    4, 64, 0, 57, 0, 48, 101, 60, 100, BT1 / 2, //1
    3, 60, 0, 55, 70,  60, 100, BT1 / 2, //1
    4, 55, 0, 60, 0,   60, 70,  60, 100, BT1 / 2, //1
    5, 48, 0, 60, 0,   60, 0, 64, 70, 62, 100, BT1 / 4, //2
    2, 62, 0, 64, 100, BT1 / 4, //3
//Em和弦
    1 + 1, 0xFF, 74, 90, 0xFF, 40, 101, BT1 / 2, //
    1 + 1, 0xFF, 64, 90, 0xFF, 55, 70, BT1 / 2,
    2 + 1, 0xFF, 63, 90, 0xFF, 55, 0, 59, 70, BT1 / 2, //0
    4 + 1, 0xFF, 64, 90, 0xFF, 40, 0, 59, 0, 64, 70,  64, 100, BT1 / 4, //3
    1, 67, 100, BT1 / 4, //5

//第三小节
//Am和弦
    4, 64, 0, 67, 0,  45, 101, 69, 100, BT1 / 2, //6
    2, 57, 70, 69, 100, BT1 / 2, //6
    4, 57, 0, 69, 0, 60, 70, 71, 100, BT1 / 3, //7
    2, 71, 0, 69, 100, BT1 / 6, //6
    3, 45, 0, 60, 0, 64, 70, BT1 / 6,
    2, 69, 0, 67, 100, BT1 / 3, //5
//Em和弦
    4 + 1, 0xFF, 74, 90, 0xFF, 64, 0, 67, 0, 40, 101, 64, 100, BT1 / 2, //3
    1, 55, 70, BT1 / 2,
    3, 55, 0, 64, 0, 59, 70, BT1 / 2, //0
    4, 40, 0, 59, 0, 64, 70, 64, 100, BT1 / 2, //3

//第四小节
//Dm和弦
    3, 64, 0,  50, 101, 62, 100, BT1 / 2, //2
    1, 57, 70, BT1 / 2,
    4, 57, 0,  62, 0, 62, 70, 62, 100, BT1 / 4, //2
    1, 60, 100, BT1 / 4, //1
    5, 62, 0, 60, 0, 50, 0, 65, 70, 57, 100, BT1 / 4, //6
    2, 57, 0, 55, 100, BT1 / 4, //5
//Am和弦
    4 + 1, 0xFF, 74, 90, 0xFF, 65, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1 + 1, 0xFF, 64, 90, 0xFF, 57, 70, BT1 / 2,
    2 + 1, 0xFF, 63, 90, 0xFF, 57, 0, 60, 70, BT1 / 2,
    3 + 1, 0xFF, 64, 90, 0xFF, 45, 0, 60, 0, 64, 70, BT1 / 4, //0
    1, 55, 100, BT1 / 4, //5

//第五小节
//F和弦
    4, 64, 0, 55, 0, 41, 101, 60, 100, BT1 / 2, //1
    1, 57, 70, BT1 / 2,
    3, 57, 0, 60, 70,  60, 100, BT1 / 2, //1
    4, 41, 0, 60, 0, 64, 70, 60, 100, BT1 / 2, //1
//G和弦
    4 + 1, 0xFF, 74, 90, 0xFF, 64, 0, 60, 0, 43, 101, 62, 100, BT1 / 2, //2
    1, 55, 70, BT1 / 2,
    2, 55, 0, 59, 70, BT1 / 2, //0
    4, 59, 0, 43, 0, 67, 70, 55, 100, BT1 / 4, //5
    2, 55, 0, 57, 100, BT1 / 4, //6

//第六小节
//C和弦
    4, 67, 0, 57, 0, 48, 101, 60, 100, BT1 / 2, //1
    3, 60, 0, 55, 70,  60, 100, BT1 / 2, //1
    4, 55, 0, 60, 0,   60, 70,  60, 100, BT1 / 2, //1
    5, 48, 0, 60, 0,   60, 0, 64, 70, 62, 100, BT1 / 4, //2
    2, 62, 0, 64, 100, BT1 / 4, //3
//Em和弦
    1 + 1, 0xFF, 74, 90, 0xFF, 40, 101, BT1 / 2, //
    1 + 1, 0xFF, 64, 90, 0xFF, 55, 70, BT1 / 2,
    2 + 1, 0xFF, 63, 90, 0xFF, 55, 0, 59, 70, BT1 / 2, //0
    4 + 1, 0xFF, 64, 90, 0xFF, 40, 0, 59, 0, 64, 70,  64, 100, BT1 / 4, //3
    1, 67, 100, BT1 / 4, //5

//第七小节
//Am和弦
    4, 64, 0, 67, 0,  45, 101, 69, 100, BT1 / 2, //6
    2, 57, 70, 69, 100, BT1 / 2, //6
    4, 57, 0, 69, 0, 60, 70, 71, 100, BT1 / 3, //7
    2, 71, 0, 69, 100, BT1 / 6, //6
    3, 45, 0, 60, 0, 64, 70, BT1 / 6,
    2, 69, 0, 67, 100, BT1 / 3, //5
//Em和弦
    4 + 1, 0xFF, 74, 90, 0xFF, 64, 0, 67, 0, 40, 101, 64, 100, BT1 / 2, //3
    1, 55, 70, BT1 / 2,
    3, 55, 0, 64, 0, 59, 70, BT1 / 2, //0
    4, 40, 0, 59, 0, 64, 70, 64, 100, BT1 / 2, //3

//第八小节
//Dm和弦
    3, 64, 0,  50, 101, 62, 100, BT1 / 2, //2
    1, 57, 70, BT1 / 2,
    4, 57, 0,  62, 0, 62, 70, 62, 100, BT1 / 4, //2
    1, 60, 100, BT1 / 4, //1
    5, 62, 0, 60, 0, 50, 0, 65, 70, 57, 100, BT1 / 4, //6
    2, 57, 0, 55, 100, BT1 / 4, //5
//Am和弦
    4 + 1, 0xFF, 74, 90, 0xFF, 65, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1 + 1, 0xFF, 64, 90, 0xFF, 57, 70, BT1 / 2,
    2 + 1, 0xFF, 63, 90, 0xFF, 57, 0, 60, 70, BT1 / 2,
    3 + 1, 0xFF, 64, 90, 0xFF, 45, 0, 60, 100, 64, 70, BT1 / 2, //1

//第九小节
//Em和弦
    4, 60, 0, 64, 0, 40, 101, 59, 100, BT1 / 2, //7
    1, 55, 70, BT1 / 2,
    4, 55, 0, 59, 0, 59, 70, 57, 100, BT1 / 2, //6
    5, 40, 0, 59, 0, 57, 0, 64, 70, 55, 100, BT1 / 2, //5
//Am和弦
    4 + 1, 0xFF, 74, 90, 0xFF, 64, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1 + 1, 0xFF, 64, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 1, 0xFF, 63, 90, 0xFF, 57, 0, 60, 70, 60, 100, BT1 / 2, //1
    4 + 1, 0xFF, 64, 90, 0xFF, 60, 0, 45, 0,  64, 70, 62, 100, BT1 / 2, //2

//第十小节
//C和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 48, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 75, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 55, 0, 67, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5, 48, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 45, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 35, 90, 0xFF, 64, 0, 57, 70, 69, 100, BT1 / 2, //6
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 69, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5, 45, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2

//第十一小节
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 62, 0, 64, 0, 40, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 75, 90, 0xFF, 64, 0, 55, 70, 71, 100, BT1 / 2, //7
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 55, 0, 71, 0,  59, 70,  71, 100, BT1 / 2, //7
    5, 40, 0, 59, 0, 71, 0, 64, 70, 67, 100, BT1 / 2, //5
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 67, 0, 45, 101, 69, 100, BT1 / 2, //6
    1 + 1, 0xFF, 35, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 60, 70, 69, 100, BT1 / 2, //6
    5, 45, 0, 60, 0, 69, 0, 64, 70, 67, 100, BT1 / 2, //5

//第十二小节
//F和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 69, 0, 41, 101, 69, 100, BT1 / 2, //6
    3 + 1, 0xFF, 75, 90, 0xFF, 69, 0, 57, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 67, 0, 60, 70, 69, 100, BT1 / 4, //6
    2, 69, 0, 67, 100, BT1 / 4, //5
    5, 41, 0, 60, 0, 67, 0, 65, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
//C和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 65, 0, 62, 0, 48, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 35, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 55, 0, 67, 0, 60, 70, 64, 100, BT1 / 2, //3
    5, 48, 0, 60, 0, 64, 0, 64, 70, 64, 100, BT1 / 2, //3

//第十三小节
//Dm和弦
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 50, 101, 62, 100, BT1 / 2, //2
    1 + 1, 0xFF, 75, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 62, 70, 60, 100, BT1 / 2, //1
    5, 50, 0, 62, 0, 60, 0, 65, 70, 62, 100, BT1 / 2, //2
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 65, 0, 62, 0, 40, 101, 64, 100, BT1 / 2, //3
    1 + 1, 0xFF, 35, 90, 0xFF, 55, 70, BT1 / 2,
    3 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 55, 0, 59, 70, 60, 100, BT1 / 2, //1
    5, 40, 0, 59, 0, 60, 0, 64, 70, 62, 100, BT1 / 2, //2

//第十四小节
//C和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 48, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 75, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 55, 0, 67, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5, 48, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 45, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 35, 90, 0xFF, 64, 0, 57, 70, 69, 100, BT1 / 2, //6
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 69, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5, 45, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2

//第十五小节
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 62, 0, 64, 0, 40, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 75, 90, 0xFF, 64, 0, 55, 70, 71, 100, BT1 / 2, //7
    4 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 55, 0, 71, 0,  59, 70,  71, 100, BT1 / 2, //7
    5, 40, 0, 59, 0, 71, 0, 64, 70, 67, 100, BT1 / 2, //5
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 67, 0, 45, 101, 69, 100, BT1 / 2, //6
    1 + 1, 0xFF, 35, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 60, 70, 69, 100, BT1 / 2, //6
    5, 45, 0, 60, 0, 69, 0, 64, 70, 67, 100, BT1 / 4, //5
    2, 67, 0, 69, 100, BT1 / 4, //6

//第十六小节
//D和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 59, 0, 50, 101, 69, 100, BT1 / 2, //6
    1 + 1, 0xFF, 75, 90, 0xFF, 57, 70, BT1 / 2,
    2 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 62, 70, BT1 / 2,
    5, 50, 0, 62, 0, 69, 0, 66, 70, 67, 100, BT1 / 2, //5
//D和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 66, 0, 67, 0, 50, 101, 69, 100, BT1 / 2, //6
    3 + 1, 0xFF, 35, 90, 0xFF, 69, 0, 57, 70, 72, 100, BT1 / 2, //1
    2 + 2, 0xFF, 37, 90, 42, 90, 0xFF, 57, 0, 62, 70, BT1 / 2,
    5, 50, 0, 62, 0, 72, 0, 66, 70, 69, 100, BT1 / 2, //6

//第十七小节
//G和弦
    4 + 1, 0xFF, 35, 90, 0xFF, 66, 0, 69, 0, 43, 101, 64, 100, BT1 / 2, //3
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    2 + 1, 0xFF, 45, 90, 0xFF, 55, 0, 59, 70, BT1 / 2,
    3 + 2, 0xFF, 43, 90, 46, 90, 0xFF, 43, 0, 59, 0, 67, 70, BT1 / 2,
//G和弦
    1 + 1, 0xFF, 42, 90, 0xFF, 43, 60, BT1 / 2,
    1 + 1, 0xFF, 46, 90, 0xFF, 55, 50, BT1 / 2,
    3 + 1, 0xFF, 42, 90, 0xFF, 55, 0, 67, 0, 59, 50, BT1 / 4,
    1, 0xFF, 46, 90, BT1 / 4,
    4 + 1, 42, 90, 0xFF, 43, 0, 59, 0, 67, 60, 55, 100, BT1 / 4, //5
    2 + 2, 0xFF, 42, 90, 46, 90, 0xFF, 55, 0, 57, 100, BT1 / 4, //6

//第十八小节
//C和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 67, 0, 57, 0, 48, 101, 60, 100, BT1 / 2, //1
    3 + 1, 0xFF, 42, 90, 0xFF, 60, 0, 55, 70,  60, 100, BT1 / 2, //1
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 60, 0,   60, 70,  60, 100, BT1 / 2, //1
    5 + 1, 0xFF, 42, 90, 0xFF, 48, 0, 60, 0,   60, 0, 64, 70, 62, 100, BT1 / 4, //2
    2, 62, 0, 64, 100, BT1 / 4, //3
//Em和弦
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 40, 101, BT1 / 2, //
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 55, 70, BT1 / 2,
    2 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 59, 70, BT1 / 2, //0
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 40, 0, 59, 0, 64, 70,  64, 100, BT1 / 4, //3
    1, 67, 100, BT1 / 4, //5

//第十九小节
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 67, 0,  45, 101, 69, 100, BT1 / 2, //6
    2 + 1, 0xFF, 42, 90, 0xFF, 57, 70, 69, 100, BT1 / 2, //6
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 69, 0, 60, 70, 71, 100, BT1 / 3, //7
    2, 71, 0, 69, 100, BT1 / 6, //6
    3 + 1, 0xFF, 42, 90, 0xFF, 45, 0, 60, 0, 64, 70, BT1 / 6,
    2, 69, 0, 67, 100, BT1 / 3, //5
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 67, 0, 40, 101, 64, 100, BT1 / 2, //3
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 55, 70, BT1 / 2,
    3 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 64, 0, 59, 70, BT1 / 2, //0
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 40, 0, 59, 0, 64, 70, 64, 100, BT1 / 2, //3

//第二十小节
//Dm和弦
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0,  50, 101, 62, 100, BT1 / 2, //2
    1 + 1, 0xFF, 42, 90, 0xFF, 57, 70, BT1 / 2,
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0,  62, 0, 62, 70, 62, 100, BT1 / 4, //2
    1, 60, 100, BT1 / 4, //1
    5 + 1, 0xFF, 42, 90, 0xFF, 62, 0, 60, 0, 50, 0, 65, 70, 57, 100, BT1 / 4, //6
    2, 57, 0, 55, 100, BT1 / 4, //5
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 65, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 57, 70, BT1 / 2,
    2 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 60, 70, BT1 / 2,
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 45, 0, 60, 100, 64, 70, BT1 / 2, //1

//第二十一小节
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 60, 0, 64, 0, 40, 101, 59, 100, BT1 / 2, //7
    1 + 1, 0xFF, 42, 90, 0xFF, 55, 70, BT1 / 2,
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 59, 0, 59, 70, 57, 100, BT1 / 2, //6
    5 + 1, 0xFF, 42, 90, 0xFF, 40, 0, 59, 0, 57, 0, 64, 70, 55, 100, BT1 / 2, //5
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 46, 90, 47, 90, 0xFF, 57, 0, 60, 70, 60, 100, BT1 / 4, //1
    1,   0xFF, 47, 90, BT1 / 4,
    4 + 2, 42, 90, 43, 90, 0xFF, 60, 0, 45, 0,  64, 70, 62, 100, BT1 / 2, //2

//第二十二小节
//C和弦
    4 + 4, 0xFF, 38, 90, 57, 90, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 48, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 42, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 67, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5 + 1, 0xFF, 42, 90, 0xFF, 48, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 45, 101, 64, 100, BT1 / 2, //3
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 57, 70, 69, 100, BT1 / 2, //6
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 69, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 45, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2

//第二十三小节
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 62, 0, 64, 0, 40, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 42, 90, 0xFF, 64, 0, 55, 70, 71, 100, BT1 / 2, //7
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 71, 0,  59, 70,  71, 100, BT1 / 2, //7
    5 + 1, 0xFF, 42, 90, 0xFF, 40, 0, 59, 0, 71, 0, 64, 70, 67, 100, BT1 / 2, //5
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 67, 0, 45, 101, 69, 100, BT1 / 2, //6
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 60, 70, 69, 100, BT1 / 2, //6
    5 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 45, 0, 60, 0, 69, 0, 64, 70, 67, 100, BT1 / 2, //5

//第二十四小节
//F和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 69, 0, 41, 101, 69, 100, BT1 / 2, //6
    3 + 1, 0xFF, 42, 90, 0xFF, 69, 0, 57, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 67, 0, 60, 70, 69, 100, BT1 / 4, //6
    2, 69, 0, 67, 100, BT1 / 4, //5
    5 + 1, 0xFF, 42, 90, 0xFF, 41, 0, 60, 0, 67, 0, 65, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
//C和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 65, 0, 62, 0, 48, 101, 64, 100, BT1 / 2, //3
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 67, 0, 60, 70, 64, 100, BT1 / 2, //3
    5 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 48, 0, 60, 0, 64, 0, 64, 70, 64, 100, BT1 / 2, //3

//第二十五小节
//Dm和弦
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 50, 101, 62, 100, BT1 / 2, //2
    1 + 1, 0xFF, 42, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 62, 70, 60, 100, BT1 / 2, //1
    5 + 1, 0xFF, 42, 90, 0xFF, 50, 0, 62, 0, 60, 0, 65, 70, 62, 100, BT1 / 2, //2
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 65, 0, 62, 0, 40, 101, 64, 100, BT1 / 2, //3
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 55, 70, BT1 / 2,
    3 + 2, 0xFF, 46, 90, 47, 90, 0xFF, 55, 0, 59, 70, 60, 100, BT1 / 4, //1
    1,   0xFF, 47, 90, BT1 / 4,
    5 + 2, 42, 90, 43, 90, 0xFF, 40, 0, 59, 0, 60, 0, 64, 70, 62, 100, BT1 / 2, //2

//第二十六小节
//C和弦
    4 + 4, 0xFF, 38, 90, 57, 90, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 48, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 42, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 2, //5
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 67, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5 + 1, 0xFF, 42, 90, 0xFF, 48, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 62, 0, 45, 101, 64, 100, BT1 / 2, //3
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 57, 70, 69, 100, BT1 / 2, //6
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 69, 0, 60, 70, 64, 100, BT1 / 4, //3
    2, 64, 0, 62, 100, BT1 / 4, //2
    5 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 45, 0, 60, 0, 62, 0, 64, 70, 60, 100, BT1 / 4, //1
    2, 60, 0, 62, 100, BT1 / 4, //2

//第二十七小节
//Em和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 62, 0, 64, 0, 40, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 42, 90, 0xFF, 64, 0, 55, 70, 71, 100, BT1 / 2, //7
    4 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 55, 0, 71, 0,  59, 70,  71, 100, BT1 / 2, //7
    5 + 1, 0xFF, 42, 90, 0xFF, 40, 0, 59, 0, 71, 0, 64, 70, 67, 100, BT1 / 2, //5
//Am和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 67, 0, 45, 101, 69, 100, BT1 / 2, //6
    1 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 57, 70, BT1 / 2,
    3 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 60, 70, 69, 100, BT1 / 2, //6
    5 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 45, 0, 60, 0, 69, 0, 64, 70, 67, 100, BT1 / 4, //5
    2, 67, 0, 69, 100, BT1 / 4, //6

//第二十八小节
//D和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 64, 0, 59, 0, 50, 101, 69, 100, BT1 / 2, //6
    1 + 1, 0xFF, 42, 90, 0xFF, 57, 70, BT1 / 2,
    2 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 62, 70, BT1 / 2,
    5 + 1, 0xFF, 42, 90, 0xFF, 50, 0, 62, 0, 69, 0, 66, 70, 67, 100, BT1 / 2, //5
//D和弦
    4 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 66, 0, 67, 0, 50, 101, 69, 100, BT1 / 2, //6
    3 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 69, 0, 57, 70, 72, 100, BT1 / 2, //1
    2 + 2, 0xFF, 38, 90, 46, 90, 0xFF, 57, 0, 62, 70, BT1 / 2,
    5 + 2, 0xFF, 35, 90, 42, 90, 0xFF, 50, 0, 62, 0, 72, 0, 66, 70, 69, 100, BT1 / 2, //6

//第二十九小节
//G和弦
    4 + 4, 0xFF, 38, 90, 35, 90, 42, 90, 57, 90, 0xFF, 66, 0, 69, 0, 43, 101, 64, 100, BT1 / 2, //3
    3 + 1, 0xFF, 42, 90, 0xFF, 64, 0, 55, 70, 67, 100, BT1 / 4, //5
    1,   0xFF, 35, 90, BT1 / 4,
    2 + 2, 42, 90, 47, 90, 0xFF, 55, 0, 59, 70, BT1 / 2,
    3 + 3, 0xFF, 46, 90, 42, 90, 57, 90, 0xFF, 43, 0, 59, 0, 67, 70, BT1 / 2,
//G和弦
    1, 43, 60, BT1 / 2,
    1, 55, 50, BT1 / 2,
    3, 55, 0, 67, 0, 59, 50, BT1 / 2,
    4, 43, 0, 59, 0, 67, 60, 55, 100, BT1 / 4, //5
    2, 55, 0, 57, 100, BT1 / 4, //6

//第三十小节
//C和弦
    4, 67, 0, 57, 0, 48, 101, 60, 100, BT1 / 2, //1
    3, 60, 0, 55, 70,  60, 100, BT1 / 2, //1
    4, 55, 0, 60, 0,   60, 70,  60, 100, BT1 / 2, //1
    5, 48, 0, 60, 0,   60, 0, 64, 70, 62, 100, BT1 / 4, //2
    2, 62, 0, 64, 100, BT1 / 4, //3
//Em和弦
    1, 40, 101, BT1 / 2, //
    1, 55, 70, BT1 / 2,
    2, 55, 0, 59, 70, BT1 / 2, //0
    4, 40, 0, 59, 0, 64, 70,  64, 100, BT1 / 4, //3
    1, 67, 100, BT1 / 4, //5

//第三十一小节
//Am和弦
    4, 64, 0, 67, 0,  45, 101, 69, 100, BT1 / 2, //6
    2, 57, 70, 69, 100, BT1 / 2, //6
    4, 57, 0, 69, 0, 60, 70, 71, 100, BT1 / 3, //7
    2, 71, 0, 69, 100, BT1 / 6, //6
    3, 45, 0, 60, 0, 64, 70, BT1 / 6,
    2, 69, 0, 67, 100, BT1 / 3, //5
//Em和弦
    4, 64, 0, 67, 0, 40, 101, 64, 100, BT1 / 2, //3
    1, 55, 70, BT1 / 2,
    3, 55, 0, 64, 0, 59, 70, BT1 / 2, //0
    4, 40, 0, 59, 0, 64, 70, 64, 100, BT1 / 2, //3

//第三十二小节
//Dm和弦
    3, 64, 0,  50, 101, 62, 100, BT1 / 2, //2
    1, 57, 70, BT1 / 2,
    4, 57, 0,  62, 0, 62, 70, 62, 100, BT1 / 4, //2
    1, 60, 100, BT1 / 4, //1
    5, 62, 0, 60, 0, 50, 0, 65, 70, 57, 100, BT1 / 4, //6
    2, 57, 0, 55, 100, BT1 / 4, //5
//Am和弦
    4, 65, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1, 57, 70, BT1 / 2,
    2, 57, 0, 60, 70, BT1 / 2,
    3, 45, 0, 60, 100, 64, 70, BT1 / 2, //1

//第三十三小节
//Em和弦
    4, 60, 0, 64, 0, 40, 101, 59, 100, BT1 / 2, //7
    1, 55, 70, BT1 / 2,
    4, 55, 0, 59, 0, 59, 70, 57, 100, BT1 / 2, //6
    5, 40, 0, 59, 0, 57, 0, 64, 70, 55, 100, BT1 / 2, //5
//Am和弦
    4, 64, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1, 57, 70, BT1 / 2,
    2, 57, 0, 60, 70, BT1 / 2,
    4, 60, 0, 45, 0, 64, 70, 57, 0, BT1 / 2,

//第三十四小节
//Dm和弦
    3, 64, 0,  50, 101, 62, 100, BT1 / 2, //2
    1, 57, 70, BT1 / 2,
    4, 57, 0,  62, 0, 62, 70, 62, 100, BT1 / 4, //2
    1, 60, 100, BT1 / 4, //1
    5, 62, 0, 60, 0, 50, 0, 65, 70, 57, 100, BT1 / 4, //6
    2, 57, 0, 55, 100, BT1 / 4, //5
//Am和弦
    4, 65, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1, 57, 70, BT1 / 2,
    2, 57, 0, 60, 70, BT1 / 2,
    3, 45, 0, 60, 100, 64, 70, BT1 / 2, //1

//第三十五小节
//Em和弦
    4, 60, 0, 64, 0, 40, 101, 59, 100, BT1 / 2, //7
    1, 55, 70, BT1 / 2,
    4, 55, 0, 59, 0, 59, 70, 57, 100, BT1 / 2, //6
    5, 40, 0, 59, 0, 57, 0, 64, 70, 55, 100, BT1 / 2, //5
//Am和弦
    4, 64, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1, 57, 70, BT1 / 2,
    2, 57, 0, 60, 70, BT1 / 2,
    4, 60, 0, 45, 0, 64, 70, 57, 0, BT1 / 2,
//第三十六小节

//Dm和弦
    3, 64, 0,  50, 101, 62, 100, BT1 / 2, //2
    1, 57, 70, BT1 / 2,
    4, 57, 0,  62, 0, 62, 70, 62, 100, BT1 / 4, //2
    1, 60, 100, BT1 / 4, //1
    5, 62, 0, 60, 0, 50, 0, 65, 70, 57, 100, BT1 / 4, //6
    2, 57, 0, 55, 100, BT1 / 4, //5
//Am和弦
    4, 65, 0, 55, 0, 45, 101, 57, 100, BT1 / 2, //6
    1, 57, 70, BT1 / 2,
    2, 57, 0, 60, 70, BT1 / 2,
//后面不直接用BT1表示，做渐慢处理
    3, 45, 0, 60, 100, 64, 70,  36, 24 / 2, //1

//第三十七小节
//Em和弦
    4, 60, 0, 64, 0, 40, 101, 59, 100, 40, 24 / 2, //7
    1, 55, 70,   44, 24 / 2,
    4, 55, 0, 59, 0, 59, 70, 57, 100,  48, 24 / 2, //6
    5, 40, 0, 59, 0, 57, 0, 64, 70, 55, 100, 52, 24 / 2, //5
//Am和弦
    4, 64, 0, 55, 0, 45, 101, 57, 100, 56, 18, //6

    3, 45, 0, 56, 0, 76 - 12, 70, BT1 / 4,
    1, 69, 70, BT1 / 4,
    1, 72, 70, BT1 / 4,
    1, 88, 70, BT1,
    4, 76, 0, 81, 0, 84, 0, 100, 0, BT1
};
extern void delay_2ms(int cnt);
void DelayXms(u32 t)
{
    t = (t > 1 ? t / 2 : 1);
    delay_2ms(t);
    wdt_clear();
}
void PlaySong(void)
{
    u32 i, k, t;
    u32 j, n;
    u8 *p;

    u8 Buf[4];

    Buf[0] = 0x09; //Note On的MIDI事件消息包头
    Buf[1] = 0x90; //在通道0上发送Note On消息

//开始播放前，关掉所有以前的声音
    Buf[3] = 0x00; //力度设置为0

    for (i = 20; i < 128; i++) {
        Buf[2] = i;


        midi_tx_data(0, Buf, 4);
    }

    DelayXms(500); //等待半秒钟安静了才开始播放

    p = (u8 *)SongTable; //歌曲音符数组

    i = 0;
//获取歌曲的行数
    k = p[i++];
    k = (k << 8) + p[i++];

//播放所有行
    while (k--) {
        //获取该行有多少个音符
        n = p[i++];

        //依次发送所有音符
        for (j = 0; j < n; j++) {
            if (p[i] == 0xFF) { //遇到0xFF则切换通道
                if (Buf[1] == 0x90) {
                    Buf[1] = 0x99;    //切换到打击乐通道
                } else {
                    Buf[1] = 0x90;    //切换回旋律乐通道
                }

                i++; //跳过本字节
            }

            //获取某一音符的音高和力度
            Buf[2] = p[i++];
            Buf[3] = p[i++];


            //发送4字节的Note On消息
            midi_tx_data(0, Buf, 4);
        }

        //计算实际的时间
        t = p[i++];
        t *= p[i++];
        //延时等待本行播放完毕
        DelayXms(t);
    }

//退出播放时，要关闭所有音符
    Buf[1] = 0x90;
    Buf[3] = 0x00;

    for (i = 20; i < 128; i++) {
        Buf[2] = i;

        midi_tx_data(0, Buf, 4);
    }
}

#endif
